<?php

	header('Access-Control-Allow-Origin: *'); //允许跨域
    //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx258fd99a593928a8&secret=7fa269ca869c067cfe6aa96ea49c24cf

	if($_GET){

		switch ($_GET['type']) {
			case 'jsapi':
				echo getJsapi_ticket($_GET['ht']);
			break;
			case 'authorization_code': //使用code调取用户信息
				echo get_authorization_code($_GET['code']);
			break;
			case 'refresh_token': //使用热刷新调取用户信息
				echo refresh_token_getuserInfo($_GET['refresh_token']);
			break;
			default:
				echo '请输入正确参数';
			break;
		}

	}

	function getJsapi_ticket($ht = 1){



		if($ht == 1){

			$qure = "SELECT toke FROM toke WHERE name_i='js_api';";

			$jsapi_ticket = getDb($qure)[0]['toke'];

			return $jsapi_ticket;

		}else{


			$qure = "SELECT toke FROM toke WHERE name_i='a_t';";

			$access_token = getDb($qure)[0]['toke'];

			$jsapi_ticket = php_do_Url_GET("https://api.weixin.qq.com/cgi-bin/ticket/getticket",["access_token"=>$access_token,"type"=>"jsapi"])['ticket'];

			if(is_null($jsapi_ticket)){ //如果 token 过期的话就重新调取

				$params=["grant_type"=>"client_credential","appid"=>"wx258fd99a593928a8","secret"=>"7fa269ca869c067cfe6aa96ea49c24cf"];

				$access_token = php_do_Url_GET("https://api.weixin.qq.com/cgi-bin/token",$params)['access_token'];

				$qureIn = "UPDATE toke SET toke = '".$access_token."' WHERE name_i='a_t';";

				insertintoDb($qureIn);

				$jsapi_ticket = php_do_Url_GET("https://api.weixin.qq.com/cgi-bin/ticket/getticket",["access_token"=>$access_token,"type"=>"jsapi"])['ticket'];

				insertintoDb("UPDATE toke SET toke = '".$jsapi_ticket."' WHERE name_i='js_api';");

				return $jsapi_ticket;

			}

			insertintoDb("UPDATE toke SET toke = '".$jsapi_ticket."' WHERE name_i='js_api';");

			return $jsapi_ticket;

		}




	}


	function get_authorization_code($CODE){


		$params=["appid"=>"wx258fd99a593928a8","secret"=>"7fa269ca869c067cfe6aa96ea49c24cf","code"=>$CODE,"grant_type"=>"authorization_code"];

		$res = php_do_Url_GET("https://api.weixin.qq.com/sns/oauth2/access_token",$params);

		$userInfo = get_wxuserinfo($res['access_token'],$res['openid']);

		$userInfo['refresh_token'] = $res['refresh_token'];


		return json_encode($userInfo,JSON_UNESCAPED_UNICODE) ;

	}


	function get_wxuserinfo($access_token,$openid){

		$params=["access_token"=>$access_token,"openid"=>$openid,"lang"=>"zh_CN"];

		$res = php_do_Url_GET("https://api.weixin.qq.com/sns/userinfo",$params);

		return $res;
	}



	function refresh_token_getuserInfo($refresh_token){


		$params=["appid"=>"wx258fd99a593928a8","refresh_token"=>$refresh_token,"grant_type"=>"refresh_token"];

		$res = php_do_Url_GET("https://api.weixin.qq.com/sns/oauth2/refresh_token",$params);

		$userInfo = get_wxuserinfo($res['access_token'],$res['openid']);

		$userInfo['refresh_token'] = $res['refresh_token'];

		return json_encode($userInfo,JSON_UNESCAPED_UNICODE) ;


	}


	function  php_do_url_POST($url,$params,$headers){ //POST get

			$ch = curl_init ();

			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
			curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
			curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
			curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSLVERSION, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

			$json_string = curl_exec ( $ch );
			$data = json_decode($json_string, true);
			curl_close ( $ch );
			return $data;
	}

	function php_do_Url_GET($url,$params=[],$headers=[]){ //Get get

		$url = "{$url}?" . http_build_query ($params);
		// echo $url;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
		//curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSLVERSION, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


		$json_string = curl_exec ( $ch );

		$data = json_decode($json_string, true);

		curl_close ( $ch );

		return $data;
	}

	function getDb($qure){ //42

		$host='101.34.87.12';
		$user='admin';
		$password='123456';
		$database='top237';
		$link=mysqli_connect($host, $user, $password, $database);
		$link->query("set names utf8");
		$result=mysqli_query($link, $qure);
		$arr=mysqli_fetch_all($result, 1);
		// $json = json_encode(array(
		// 		"code"=>0,
		// 		"msg"=>count($arr),
		// 		"data"=>$arr),
		// 		JSON_UNESCAPED_UNICODE);

		mysqli_close($link);

		return $arr;

	}

	function insertintoDb($qure){


		$host='101.34.87.12';
		$user='admin';
		$password='123456';
		$database='top237';
		$link=mysqli_connect($host, $user, $password, $database);
		$link->query("set names utf8");
		$result=mysqli_query($link, $qure);


		if ($result === TRUE) {

			return $json = json_encode(array(
				"code"=>300,
				"msg"=>'新记录插入成功'
				),
				JSON_UNESCAPED_UNICODE);


		} else {

			return $json = json_encode(array(
					"code"=>400,
					"msg"=>$link->error,
					"qure"=>$qure),
					JSON_UNESCAPED_UNICODE);

		}

		mysqli_close($link);


}

?>
